/*
 * @Author: wuqingchun
 * @Date: 2024-12-13 09:05:34
 * @Description: C++ 文件操作
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-12-15 15:37:30
 */
#include <cerrno>
#include <cstring>
#include <iostream>
#include <fstream>
#include <string>
#include <sys/time.h>
#include <chrono>


inline int64_t DateTime2MicroSec(int32_t iDate, int32_t iTime)
{
    int32_t iYear = iDate / 10000;
    int32_t iMon = iDate % 10000 / 10000;
    int32_t iDay = iDate % 100;
    // 创建一个tm结构体并填充日期和时间
    std::tm stTM = {};
    stTM.tm_year = iDate - 1900;             // 年份从1900年开始计算
    stTM.tm_mon = iMon - 1;                  // 月份从0开始计算
    stTM.tm_mday = iDay;                     // 日
    stTM.tm_hour = iTime / 10000;            // 小时
    stTM.tm_min = iTime % 10000/100;         // 分钟
    stTM.tm_sec = iTime % 100;               // 秒

    std::cout << iYear << iMon << iDay << " " << stTM.tm_hour << ":" << 
    stTM.tm_min << ":" << stTM.tm_sec << std::endl;

    // 将tm结构体转换为time_t
    std::time_t stTime = std::mktime(&stTM);

    // 将time_t转换为system_clock::time_point
    std::chrono::system_clock::time_point time_point = std::chrono::system_clock::from_time_t(stTime);

    // 获取自UNIX纪元以来的微秒数
    auto i64Microseconds = std::chrono::duration_cast<std::chrono::microseconds>(time_point.time_since_epoch()).count();

    // 输出微秒数
    std::cout << "Microseconds since UNIX epoch: " << i64Microseconds << std::endl;

    return i64Microseconds;
}

inline uint64_t get_current_micro()
{
    struct timeval stv;
    gettimeofday(&stv, NULL);
    std::cout << stv.tv_sec << ":" << stv.tv_usec << std::endl;
    return (uint64_t)(stv.tv_sec * 1000000) + stv.tv_usec;
}


//获取出来是微妙的时间
inline uint64_t get_current_micro_time()
{
	struct timespec tp = { 0 };
	clock_gettime(CLOCK_MONOTONIC, &tp);
    std::cout << tp.tv_sec << ":" << tp.tv_nsec << std::endl;
	return (((uint64_t)tp.tv_sec) * 1000000000 + tp.tv_nsec)/1000;
}



void get_current_time()
{
    // 获取当前时间
    std::time_t now = std::time(nullptr);

    // 将当前时间转换为tm结构体（UTC时间）
    std::tm* now_tm = std::gmtime(&now);

    // 获取时、分、秒
    int hour = now_tm->tm_hour;
    int minute = now_tm->tm_min;
    int second = now_tm->tm_sec;

    // 输出时、分、秒
    std::cout << "Current time (UTC): " << hour << ":" << minute << ":" << second << std::endl;

    // 如果需要本地时间，可以使用localtime
    now_tm = std::localtime(&now);

    // 获取本地时、分、秒
    hour = now_tm->tm_hour;
    minute = now_tm->tm_min;
    second = now_tm->tm_sec;

    // 输出本地时、分、秒
    std::cout << "Current time (Local): " << hour << ":" << minute << ":" << second << std::endl;
}


void get_current_time_cpp()
{
    // 获取当前时间点
    std::chrono::system_clock::time_point now = std::chrono::system_clock::now();

    // 将当前时间点转换为time_t值
    std::time_t now_time_t = std::chrono::system_clock::to_time_t(now);

    // 将time_t值转换为tm结构体（UTC时间）
    std::tm* now_tm_utc = std::gmtime(&now_time_t);

    // 获取时、分、秒
    int hour_utc = now_tm_utc->tm_hour;
    int minute_utc = now_tm_utc->tm_min;
    int second_utc = now_tm_utc->tm_sec;

    // 输出UTC时、分、秒
    std::cout << "Current time (UTC): " << hour_utc << ":" << minute_utc << ":" << second_utc << std::endl;

    // 将time_t值转换为tm结构体（本地时间）
    std::tm* now_tm_local = std::localtime(&now_time_t);

    // 获取本地时、分、秒
    int hour_local = now_tm_local->tm_hour;
    int minute_local = now_tm_local->tm_min;
    int second_local = now_tm_local->tm_sec;

    // 输出本地时、分、秒
    std::cout << "Current time (Local): " << hour_local << ":" << minute_local << ":" << second_local << std::endl;

}

int main(int argc, char** argv)
{
    std::fstream file;
    file.open("test.txt", std::ios::out);
    if (!file.is_open())
    {
        std::cout << "open file error: " << strerror(errno) << std::endl;

        return 0;
    }

    file << "aaaaaa\n";
    file << "bbbb\n";
    file << "ccc\n";
    file.close();

    file.open("test.txt", std::ios::in);
    if (!file.is_open())
    {
        std::cout << "open file error: " << strerror(errno) << std::endl;

        return 0;
    }
    std::string strLine;
    uint32_t uiLineNo = 1;
    while (std::getline(file, strLine))
    {
        std::cout << uiLineNo++ << " " << strLine << std::endl;
    }

    file.close();

    std::ofstream fileout;
    fileout.open("test.txt", std::ios::trunc);
    if (!fileout.is_open())
    {
        std::cout << "open file error: " << strerror(errno) << std::endl;

        return 0;
    }

    fileout.close();


    DateTime2MicroSec(20241215, 151850);

    std::cout << get_current_micro_time() << std::endl;
    std::cout << get_current_micro() << std::endl;


    get_current_time();

    get_current_time_cpp();

    return 0;
}

